$(document).ready(function () {
	$(document).on("click",".register_link",function () {
		$(document).on('keydown.tab',"input",function(e){
			if(e.keyCode == 9){
				e.preventDefault();
			}
		});
		$(".form-login").animate({height:"150px"},500);
		$(".page_login").animate({width:"40%"},500);
		$(".logo span").fadeOut(250,function () {
			$(this).text("用户注册");
			$(this).fadeIn(250,function(){
				$(".page_register input[type!=hidden]").eq(0).focus();
				$(document).off('keydown.tab')
			});
		});
		$("#province").trigger("change");
	})
	.on("click",".backtologin_link",function () {
		$(document).on('keydown.tabs',"input",function(e){
			if(e.keyCode ==9){
				e.preventDefault();
			}
		});
		$(".form-login").animate({height:"310px"},500);
		$(".page_login").animate({width:"100%"},500);
		$(".logo span").fadeOut(250,function () {
			$(this).text("用户登陆");
			$(this).fadeIn(250,function(){
				$(".page_login input[type!=hidden]").eq(0).focus();
				$(document).off('keydown.tabs')
			});
		});
	})
	.on("blur","input",function () {
		var $this = $(this),
			unameReg = /^[0-9A-Za-z]{1}[_0-9A-Za-z]+$/,
			emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/,
			form = $(this).closest(".form"),
			type = $(this).attr("name"),
			text = $(this).val();
			;
		switch (type){
			case 'user':
				if ( !unameReg.test(text) && !emailReg.test(text) ) $this.addClass("error");
				else $this.removeClass("error");
				break;
			case 'upw':
				if ( !text ){
					$this.addClass("error");
				}
				else $this.removeClass("error");
				break;
			case 'uname':
				if (  !unameReg.test(text) ){
					$this.addClass("error");
				}
				else $this.removeClass("error");
				break;
			case 'email':
				if (  !emailReg.test(text) ){
					$this.addClass("error");
				}
				else $this.removeClass("error");
				break;
			case 'repwd':

				if (  text !== form.find("input[name=upw]").val() ){
					$this.addClass("error");
				}
				else $this.removeClass("error");
				break;
			default:
				return false;
		}

	})
	.on('click','.login_btn',function () {
		var $elem = $(this),
 			form = $elem.closest("form"),
 			elems = form[0].elements,
 			data = {action:"login"},
 			callback_url = "http://localhost:8080/gd_gdxt/gd_be/index.php"
 			;
 		if( form.find('.error').length != 0 ){
 			alert("用户名或密码格式不正确");
 			return;
 		}

 		for(var i=0,len=elems.length;i<len;i++){
	 		if ( !elems[i].value ){
	 			alert("用户名或密码格式不正确");
	 			return;
	 		}
	 		data[elems[i].name] = elems[i].value;
	 	}

	 	data["upw"] = $.md5(data["upw"]);

 		$.ajax({
 			url: "api.php",
 			type: "POST",
 			data: data,
 			dataType: "json",
 			success: function (data) {
 				if( data.status!=0 ){
 					alert(data.message);
 					return;
 				}
 				else{
 					if( callback_url.indexOf("?")==-1 ){
 						callback_url += "?identify="+data.data;
 					}
 					else {
 						callback_url += "&identify="+data.data;
 					}
 					window.location.href = callback_url;
 				}
 			},
 			error: function (e) {
 				console.log(e);
 			}
 		});
	})
	.on('click','.register_btn',function () {
		var $elem = $(this),
 			form = $elem.closest("form"),
 			elems = form[0].elements,
 			data = {action:"register"}
 			;
 		if( form.find('.error').length != 0 ){
 			alert("红框内容格式不正确");
 			return;
 		}

 		for(var i=0,len=elems.length;i<len;i++){
 			if ( !elems[i].value ){
	 			alert("注册信息不完整");
	 			return;
	 		}
	 		data[elems[i].name] = elems[i].value;
	 	}
	 	
 		$.ajax({
 			url: "api.php",
 			type: "POST",
 			data: data,
 			dataType: "json",
 			success: function (data) {
 				if( data.status!=0 ){
 					alert(data.message);
 					return;
 				}
 				else{
 					alert("请在邮件中激活您的账户，方可登陆");
 					$(".backtologin_link").click();
 					return;
 				}
 			},
 			error: function (e) {
 				console.log(e);
 			}
 		});
	})
	.on("keydown","input",function(e){
		if (e.keyCode == 9) {
			e.preventDefault();
			var inputSelect = $(this).closest("form").find('input[type!=hidden]'),
				curElem = inputSelect.index(this),
			    formLen = inputSelect.length;
			    inputSelect[(curElem+1)%formLen].focus();
		}
	})
	.on("keyup","input",function(e){
		if(e.keyCode == 13){
			$(this).closest("form").find("a[data-type=submit]").click();
		}
	})
	.on('change','#province',function () {
		//alert(111);
		var pid = $('#province').val();
		$("#OutletId").val(pid);
		//alert(pid);
		$('#city').empty();
		$('#town').empty();
		$.ajax({
			type:"POST",
			async:false,
			url:"http://localhost:8080/gd_gdxt/gd_uc/api.php?action=region",
			data:{region_id : pid, region_type: 2},
			dataType:"json",
			success:function(rs){
				//alert(rs.data.length);
				changeLoc(rs , "city");
				if(rs.data.length!=0){
					$("#OutletId").val(rs.data[0].region_id);
						$.ajax({
							type:"POST",
							async:false,
							url:"http://localhost:8080/gd_gdxt/gd_uc/api.php?action=region",
							data:{region_id: rs.data[0].region_id, region_type: 3},
							dataType:"json",
							success:function(rs){
								changeLoc(rs , "town");
								if(rs.data.length!=0&&rs.data[0].region_id!=-1){
									$("#OutletId").val(rs.data[0].region_id);
								}
							}
						});//ajax
				}
			}
		});	
	})
	.on('change',"#city",function () {
		var pid = $('#city').val();
		if(pid==-1){
			$("#OutletId").val($('#province').val());
		}else{
			$("#OutletId").val(pid);
		}
		
		$('#town').empty();
		$.ajax({
			type:"POST",
			async:false,
			url:"http://localhost:8080/gd_gdxt/gd_uc/api.php?action=region",
			data:{region_id: pid, region_type: 3},
			dataType:"json",
			success:function(rs){
				changeLoc(rs , "town");
				if(rs.data.length!=0&&rs.data[0].region_id!=-1){
					$("#OutletId").val(rs.data[0].region_id);
				}
			}
		});
	})
	.on('change',"#town",function () {	
		var pid = $('#town').val();
		if(pid==-1){
			if($('#city').val()==-1)
				$("#OutletId").val($('#province').val());
			else
				$("#OutletId").val($('#city').val());
		}else{
			$("#OutletId").val(pid);
		}
	});

	function changeLoc (rjson , rform) {
		var varHtml = "";
		if(rjson.data.length!=0){
			for(var i=0;i<rjson.data.length;i++){
				varHtml+="<option value='"+rjson.data[i].region_id+"'>"+rjson.data[i].region_name+"</option>";
			}
		}
		$('#'+rform).html(varHtml);
	}
	//$("#province").val("13");
	
	
});